<template>
	<view>
		<view class="title">
			<image :src="switchImg(gImages)" class="goods-img"></image>
			<view style="margin-top: 15rpx;">
				<view class="qian" style="font-size: 20px;">{{orderMoney}}</view>
				<view class="comany">携联健康养生</view>
			</view>
		</view>
		<view style="margin-top: 25rpx;background-color: white;">
			<view style="display: flex;padding: 20rpx 0 20rpx 0;justify-content:space-around;">
				<view style="margin-top: 15rpx;"><image src="../../static/center/jiangjin.png" style="width: 40rpx;height: 40rpx;"></image></view>
				<view>
					<view style="font-size: 16px;">积分支付 </view>
					<view style="font-size: 11px; color: #C7C7C7;margin-top: 7rpx;">
						<span>可用积分 ¥<span>{{usersIntegral}}</span></span>
					</view>
				</view>
				<view style="margin-top: 15rpx;">
					<input @input="inputJifen" v-model="jifen" placeholder="请输入积分" type="digit" style="padding-bottom: 20rpx;font-size: 12px;width: 130px;border:none;border-bottom: 1px solid #cccccc;">
				</view>
			</view>
		</view>
		<view style="margin-top: 40rpx;">
			<button type="default" class="confirm-btn" @click="querenPay()">确认支付 <text class="qian">{{money}}</text></button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ddhao:"",		// 中间的订单编号
				money:0,		// 应付金额
				gImages:"", 	// 商品图片
				userinfo:[],	// 用户信息
				orderMoney:0,	// 订单金额
				jifen:'',		// 输入的积分
				id:'',			// 类型（1：立即购买支付   2：购物车结算支付）
				usersIntegral:0,// 用户可用积分
				gid:'',			// 商品ID
			}
		},
		methods: {
			// 设置图片路径
			switchImg(s){
				return this.host+s;
			},
			//输入积分
			inputJifen(e){
				if(e.target.value < 0){
					this.$u.toast("积分不能小于0")
					return
				}else{
					let jifen = this.orderMoney - e.target.value;
					if(jifen < 0){
						this.$u.toast("积分超出使用范围")
						this.money = this.orderMoney;
						this.jifen = '';
						return
					}else{
						this.money = jifen.toFixed(2);
					}
				}
			},
			//品牌共享订单确认支付
			querenPay(){
				console.log("点击确认支付了")
				let userid = uni.getStorageSync('usersId');
				let openId = uni.getStorageSync('openId');
				if(this.jifen>0 && this.money>0){
					console.log("混合支付")
					this.$http.get('/ppOrderPay/hunhePayxcx',{
						userId:userid,
						ddhao:this.ddhao,
						jifen:this.jifen,
						money:this.money,
						openId:openId
					}).then(res => {
						console.log(res.data)
						if(res.data.code==200){
							this.ddhao = res.data.data.ddhao;
							this.pay(res)//调用微信支付
						}else{
							this.$u.toast(res.data.msg)
						}
					})
				}else if(this.jifen==null || this.jifen=="" || this.jifen==0){
					console.log("微信支付")
					this.$http.get('/ppOrderPay/weixinPayxcx',{
						userId:userid,
						ddhao:this.ddhao,
						money:this.money,
						openId:openId
					}).then(res => {
						console.log(res.data)
						if(res.data.code==200){
							this.ddhao = res.data.data.ddhao;
							this.pay(res)//调用微信支付
						}else{
							this.$u.toast(res.data.msg)
						}
					})
				}else{
					console.log("积分支付")
					if(this.jifen*1 == this.orderMoney*1){
						uni.navigateTo({
							url:'payPwd?ddhao='+this.ddhao+'&jifenPay='+this.jifen+'&payPrice='+this.orderMoney
						})
					}else{
						this.$u.toast("输入积分需等于应付金额！")
					}
				}
			},
			//支付
			pay(res){
				let that = this;
				uni.requestPayment({
					appid: res.data.data.appId,  			// AppId，注意微信小程序、公众号 AppId 可能不一致
					nonceStr: res.data.data.nonceStr, 		// 随机字符串
					package: res.data.data.package,         // 固定值
					signType: res.data.data.signType,		// 签名类型
					timeStamp: res.data.data.timeStamp,     // 时间戳（单位：秒）
					paySign:res.data.data.paySign,
				    success(res) {
						uni.showToast({
							title: '支付成功',
							icon: 'success'
						});
						//支付成功修改订单状态
						that.$http.get('/ppOrderPay/ppPayNotifyxcx',{
							orderId:that.ddhao
						}).then(res => {
							//支付成功，跳转页面
							uni.$emit('checkPinpaiUserInfo');
							uni.$emit('getPinpaiOrderByGroup');
							// uni.navigateBack(2)
							uni.redirectTo({
								url:'../center/center'
							})
						})
					},
				    fail(e) {
						uni.showToast({
							title: '支付失败',
							icon: 'error'
						});
					}
				})
			},
			//得支付信息
			getPayInfo(id){
				if(id==1){//商品详情页立即支付
					this.$http.get('/ppOrder/getGoodsDetailPay',{
						userid:uni.getStorageSync('usersId'),
						unionid:this.ddhao
					}).then(res => {
						this.usersIntegral = res.data.jifen;
						if(res.data.money <= 0){
							this.money = 0.01;
						}else{
							this.money = res.data.money;
						}
						if(res.data.orderMoney <= 0){
							this.orderMoney = 0.01;
						}else{
							this.orderMoney = res.data.money;
						}
					})
				}else if(id==2){//购物车页面结算支付
					this.$http.get('/ppOrder/getCatOrderPay',{
						userid:uni.getStorageSync('usersId'),
						unionid:this.ddhao
					}).then(res => {
						this.usersIntegral = res.data.jifen;
						if(res.data.money <= 0){
							this.money = 0.01;
						}else{
							this.money = res.data.money;
						}
						if(res.data.orderMoney <= 0){
							this.orderMoney = 0.01;
						}else{
							this.orderMoney = res.data.money;
						}
					})
				}else if(id==3){//订单页面支付
					this.$http.get('/ppOrder/getOrderPay',{
						userid:uni.getStorageSync('usersId'),
						unionid:this.ddhao
					}).then(res => {
						this.usersIntegral = res.data.jifen;
						if(res.data.money <= 0){
							this.money = 0.01;
						}else{
							this.money = res.data.money;
						}
						if(res.data.orderMoney <= 0){
							this.orderMoney = 0.01;
						}else{
							this.orderMoney = res.data.money;
						}
					})
				}
			}
		},
		onLoad(opt){
			this.ddhao = opt.orid;
			this.gImages = opt.gimages;
			this.money = opt.money;
			this.orderMoney = opt.money;
			this.gid = opt.gid;
			this.id = opt.id;
			this.getPayInfo(opt.id);
			this.userinfo = uni.getStorageSync('userinfo');
		},
	}
</script>

<style scoped>
	.title{
		display: flex;
		justify-content: center;
		background-color: white;
		padding: 20rpx 0rpx;
		margin-bottom: 20rpx;
	}
	.comany{
		font-size: 16rpx;
	}
	.title>view{
		margin-left: 30rpx;
	}
	.title>view>view{
		margin-top: 10rpx;
	}
	.qian{
		/* font-weight: bold; */
		font-size: 32rpx;
	}
	.confirm-btn .qian{
		font-size: 30rpx;
	}
	.confirm-btn{
		background-color: #ff9c05;
		color: white;
		width: 90%;
		margin: 0px auto;
		height: 70rpx;
		font-size: 26rpx;
		line-height: 70rpx;
	}
	.goods-img{
		width: 100rpx;
		max-height: 100rpx;
		margin: 20rpx 0rpx;
		border-radius: 200rpx;
	}
	.qian::before{
		content: '￥';
	}
</style>
